## Modul: PotGuiFontSelector ##

import CmdNo,string
from etgui import *
def InitFonts():
	fonts=[
		# display		model				et++
		("System", 		"SysFont",			eFontSys),
		("Application", "AppFont",			eFontApp),
		("Fixed", 		"FixedFont",		eFontFix),
		("Times", 		"Times",			eFontTimes),
		("Helvetica", 	"Helvetica",		eFontHelvetica),
		("Courier", 	"Courier",			eFontCourier),
		("Symbol", 		"Symbol",			eFontSymbol),
		("Chicago", 	"Chicago",			eFontChicago),
		("Avantgarde", 	"Avantgarde",		eFontAvantgarde),
		("Bookman", 	"Bookman",			eFontBookman),
		("Schoolbook", 	"Schoolbook",		eFontSchoolbook),
		("NHelvetica", 	"NarrowHelvetica",	eFontNHelvetica),
		("Palatino", 	"Palatino",			eFontPalatino),
		("Chancery", 	"ZapfChancery",		eFontChancery),
		("Dingbats", 	"ZapfDingbats",		eFontDingbats),
	]
	id=eFontDingbats+1
	while 1:
		name=FontIdToName(id)
		if not name:
			break
		fonts.append((name,name,id))
		id=id+1
	return fonts
gFonts=InitFonts()
gStyles=[
		# display		model			et++
		("Regular",		"Medium",		eFacePlain),
		("Italic",		"Oblique",		eFaceItalic),
		("Bold",		"Bold",			eFaceBold),
		("BoldItalic",	"BoldOblique",	eFaceBold|eFaceItalic),
	]

def GetInkFromModel(model,path):
	r=model.GetValue(path+'.Red') or 0
	g=model.GetValue(path+'.Green') or 0
	b=model.GetValue(path+'.Blue') or 0
	return RGBInk(r,g,b)

def GetFontFromModel(model,path):
	font_name=model.GetValue(path+'.Font')
	font=eFontSys
	for d,m,f in gFonts:
		if m==font_name:
			font=f
			break
	size=model.GetValue(path+'.Size') or 0
	style_name=model.GetValue(path+'.Style') or 9
	style=eFacePlain
	for d,m,s in gStyles:
		if m==style_name:
			style=s
			break
	if model.GetValue(path+'.Underline'):
		style=style|eFaceUnderline
	return Font(
		font,
		size,
		style
	)

class FontBoxObj:
	# 'static' variables holding the font/face tables
	fonts=gFonts
	styles=gStyles
	def __init__(self,model,path,color_path=None,use_preview=1,auto=0):
		self.model=model
		self.path=path
		self.color_path=color_path
		self.use_preview=use_preview
		self.automatic=auto
		self.font_font=eFontTimes
		self.font_face=eFacePlain
		self.font_underline=0
		self.font_size=12
		self.ink=None
		self.min_size_val=9
		self.max_size_val=24
	def GetSlot(self,model,slot):
		return model.GetValue(self.path+'.'+slot)
	def SetFont(self,name):
		for d,m,font in gFonts:
			if m==name:
				self.font_font=font
				return
	def SetUnderline(self,v):
		if v:
			self.font_underline=eFaceUnderline
		else:
			self.font_underline=0
	def SetSize(self,v):
		if v is not None:
			self.font_size=v
	def SetStyle(self,v):
		for d,m,face in gStyles:
			if m==v:
				self.font_face=face
				return
	def UpdatePreview(self):
		if self.use_preview:
			self.textitem.SetFont(
				Font(
					self.font_font,
					self.font_size,
					self.font_face|self.font_underline
				)
			)
		if self.ink:
			self.textitem.SetInk(self.ink)
		#self.vobj.CalcExtent()
		self.vobj.Update()
	def InitAll(self,model=None):
		if model is None:
			model=self.model
		self.SetFont(self.GetSlot(model,"Font"))
		self.SetUnderline(self.GetSlot(model,"Underline"))
		self.SetSize(self.GetSlot(model,"Size"))
		self.SetStyle(self.GetSlot(model,"Style"))
		if self.color_path is not None:
			self.ink=GetInkFromModel(model,self.color_path)
		self.UpdatePreview()
	def ColorChanged(self,model,slot):
		self.ink=GetInkFromModel(Cast(model),self.color_path)
		self.UpdatePreview()
	def PreviewChanged(self,model,slot):
		#print "PreviewChanged",(slot,self.path)
		model=Cast(model)
		if slot==self.path or slot==self.path+'.':
			self.InitAll(model)
			return
		new=model.GetValue(slot)
		path=slot
		# find the last field (after the last '.')
		i=string.rfind(slot,'.')
		if self.path!=slot[:i]:
			print "FontBoxObj.PreviewChanged->OOOOOPS".self.path,slot
			return # it is not for us...
		slot=slot[i+1:]
		if slot == "Style":
			self.SetStyle(new)
		elif slot == "Font":
			self.SetFont(new)
		elif slot == 'Size':
			self.SetSize(new)
		elif slot == 'Underline':
			self.SetUnderline(new)
		self.UpdatePreview()
	def MakeVObj(self):
		path=self.path+'.'
		fonts=PotGuiPopupButton(path+"Font")
		if self.automatic:
			fonts.AddItem("(None)","auto")
		for d,m,et in gFonts:
			fonts.AddItem(d,m)
		style=PotGuiPopupButton(path+"Style")
		if self.automatic:
			style.AddItem("(None)","auto")
		for d,m,et in gStyles:
			style.AddItem(d,m)
		size=PotGuiPopupButton(path+"Size")
		if self.automatic:
			size.AddItem("(None)",0)
		for i in range(self.min_size_val,self.max_size_val+1):
			size.AddItem(str(i),i)
		colorbox=underline=previewbox=Filler(gPoint0)   # only on demand
		if self.color_path is not None:
			colorbox=VBox(align=eVObjHFixed|eVObjVFixed, 
				items=[ 
					TextItem("Color"), 
					PotGuiColorSelector(self.color_path,Point(96,16))
				]
			)
			underline=PotGuiToggleButton(path+"Underline","Underline")
		if self.use_preview:
			self.textitem=StyledTextItem(-1,"AaBbCc..XxYyZz 0123456789")
			previewbox=AlignBox(
				eVObjHExpand,
				BorderItem(
					"Preview",
					Clipper(
						self.textitem,
						Point(100,26), # minsize of the preview
						gInkNone       # background ink (use gClipperBgColor)
					)
				)
			)
		self.vobj=VBox(align=eVObjHExpand|eVObjVExpand,
			items=[
				HBox(align=eVObjHExpand|eVObjVExpand,
					items=[
						VBox(align=eVObjHFixed|eVObjVFixed,
							items=[
								TextItem("Font"),
								fonts,
								underline,
							]
						),
						VBox(align=eVObjHFixed|eVObjVFixed,
								items=[
									TextItem("Font Style"),
									style,
									colorbox
								]
						),
						VBox(align=eVObjHFixed|eVObjVFixed,
								items=[
									TextItem("Size"),
									size,
								]
						)  
					]
				),
				previewbox
			]
		)
		self.model.AddModelObserver(self.PreviewChanged,path,self.vobj)
		if self.color_path:
			self.model.AddModelObserver(self.ColorChanged,self.color_path,self.vobj)
		return self.vobj

def FontBox(model,path,color_path=None,use_preview=1,auto=0):
	return FontBoxObj(model,path,color_path,use_preview,auto).MakeVObj()
